<!--
 * @Author: wangshuguang
 * @Date: 2022-02-17 10:49:37
 * @LastEditTime: 2022-04-27 16:27:17
 * @LastEditors: wangshuguang
 * @Description:  支出记录
-->
<template>
  <div class="common-container">
    <!-- 筛选条件 -->
    <el-card>
      <el-form ref="filterForm" :model="listQuery" class="demo-form-inline" label-width="100px">
        <el-row>
          <el-col :span="24" prop="date">
            <el-form-item label="时间">
              <el-radio-group v-model="filterDate" size="small" @change="dataRadioChange">
                <el-radio-button :label="1">今天</el-radio-button>
                <el-radio-button :label="2">昨天</el-radio-button>
                <el-radio-button :label="7">近7天</el-radio-button>
                <el-radio-button :label="15">近15天</el-radio-button>
                <el-radio-button :label="30">近30天</el-radio-button>
                <el-radio-button label="nowMonth">本月</el-radio-button>
              </el-radio-group>&nbsp;
              <el-date-picker
                v-model="filterTime"
                :clearable="false"
                format="yyyy-MM-dd HH:mm:ss"
                value-format="yyyy-MM-dd HH:mm:ss"
                :default-time="['00:00:00', '23:59:59']"
                size="small"
                type="daterange"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                @change="pickerChange"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="交易状态" prop="flowSts">
              <el-select v-model="listQuery.flowSts" clearable placeholder="请选择" size="small" class="width100">
                <el-option label="全部" value="" />
                <el-option label="交易成功" value="S" />
                <el-option label="交易失败" value="F" />
                <el-option label="交易中" value="W" />
              </el-select>
            </el-form-item>
          </el-col>
          <div v-show="showMoreFilter">
            <el-col :span="12">
              <el-form-item label="业务类型" prop="expendType">
                <el-select v-model="listQuery.expendType" clearable placeholder="请选择" size="small" class="width100">
                  <el-option label="全部" value="" />
                  <el-option label="购买流量余额" value="1" />
                  <el-option label="商户开通插件" value="1000" />
                  <el-option label="购买合作商户名额" value="1001" />
                  <el-option label="插件续费" value="1003" />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="所属插件" prop="pluginCode">
                <el-select v-model="listQuery.pluginCode" clearable placeholder="请选择" size="small" class="width100">
                  <el-option label="全部" value="" />
                  <el-option label="未使用插件" value="0" />
                  <el-option label="超级会员" value="supermember" />
                  <el-option label="储值AI" value="2" />
                  <el-option label="满减满折" value="discount" />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="金额区间" prop="beginAmt">
                <el-input v-model="listQuery.beginAmt" placeholder="最低价" type="number" style="width:100px" :controls="false" size="small" />
                <span>&nbsp;&nbsp;-&nbsp;&nbsp;</span>
                <el-input v-model="listQuery.endAmt" placeholder="最高价" type="number" style="width:100px" :controls="false" size="small" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="支付单号" prop="paymentNo">
                <el-input v-model="listQuery.paymentNo" clearable placeholder="请输入" size="small" class="width100" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="业务单号" prop="orderNo">
                <el-input v-model="listQuery.orderNo" clearable placeholder="请输入" size="small" class="width100" />
              </el-form-item>
            </el-col>
          </div>
          <el-col :span="12">
            <el-form-item>
              <el-button type="primary" round size="small" @click="fetchData">查询</el-button>
              <el-button type="primary" round size="small" @click="resetForm('filterForm')">重置</el-button>
              <el-button type="text" round size="small" @click="showMoreFilter = !showMoreFilter">{{ showMoreFilter ? '收起高级选项' : '展开高级选项' }}</el-button>
              <el-button type="text" round size="small" @click="downFile">导出为excel</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-card>
    <div class="dataList">
      <el-table size="medium" :data="list" border>
        <el-table-column prop="roleTypeZh" align="center" label="序号">
          <template slot-scope="scope">{{ scope.$index + 1 }}</template>
        </el-table-column>
        <el-table-column prop="paymentNo" align="center" label="支付单号" />
        <el-table-column prop="paymentNo" align="center" label="三方支付单号" />
        <el-table-column prop="typeZh" align="center" label="业务类型" />
        <el-table-column prop="orderNo" align="center" label="业务单号" />
        <el-table-column prop="pluginCodeZh" align="center" label="所属插件" />
        <el-table-column prop="payAmount" align="center" label="交易金额/元" />
        <el-table-column prop="flowStsZh" align="center" label="支付状态" />
        <el-table-column prop="createTime" align="center" label="交易时间" />
        <el-table-column prop="remark" align="center" label="备注" width="200" />
        <el-table-column prop="statisticalTime" align="center" label="操作">
          <template slot-scope="scope">
            <el-button type="text" @click="viewData(scope.row)">详情</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination-container">
        <el-pagination
          :current-page.sync="listQuery.pageNum"
          :page-size="listQuery.pageSize"
          :total="total"
          layout="total, prev, pager, next"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>

    <!-- 详情弹框 -->
    <el-dialog
      title="交易订单详情"
      :visible.sync="orderDetailVisible"
      width="40%"
    >
      <ExpendHisDetail v-if="orderDetailVisible" :info-config="infoConfig" />
    </el-dialog>
  </div>
</template>

<script>
import { qryExpendRecord, exportMercExpend } from '@/api/payCenter'
import { mapGetters } from 'vuex'
import ExpendHisDetail from '../components/ExpendHisDetail.vue'
import { dateFtt, removePropertyOfNull } from '@/utils'
import { getFilterFormPastTime } from '@/utils/format'

export default {
  components: { ExpendHisDetail },
  data() {
    return {
      orderDetailVisible: false,
      showMoreFilter: false,
      infoVisible: false,
      infoConfig: {},
      form: {},
      scope: 0,
      total: 0,
      filterDate: 1,
      filterTime: [],
      listQuery: {
        pageNum: 1,
        pageSize: 10,
        name: undefined,
        flowSts: '',
        expendType: '',
        pluginCode: '',
        orderNo: '',
        paymentNo: '',
        beginAmt: '',
        source: '',
        endAmt: '',
        beginTime: '',
        endTime: ''
      },
      list: []
    }
  },

  computed: {
    ...mapGetters(['businessInfo'])
  },
  async mounted() {
    // 设置时间查询默认值
    this.filterDate = 1
    this.dataRadioChange(1)
  },
  methods: {
    viewData(data) {
      this.infoConfig = data
      this.orderDetailVisible = true
    },
    pickerChange(val) {
      // 时间筛选条件切换
      console.log('pickerChange', val)
      this.listQuery.beginTime = val[0]
      this.listQuery.endTime = val[1]
      this.filterDate = ''
      this.fetchData()
    },
    async dataRadioChange(val) {
      // 时间tab切换
      let dateArr = []
      if (val == 'nowMonth') {
        dateArr = await getFilterFormPastTime('nowMonth')
      } else if (val == 2) {
        dateArr = await getFilterFormPastTime('yesterday')
      } else {
        dateArr = await getFilterFormPastTime(val)
      }
      this.listQuery.beginTime = dateArr[0]
      this.listQuery.endTime = dateArr[1]
      console.log('val', val, dateArr)
      this.filterTime = []
      this.fetchData()
    },
    // 重置
    resetForm(formName) {
      this.filterDate = 1
      this.listQuery.endAmt = ''
      this.listQuery.beginAmt = ''
      this.$refs[formName].resetFields()
      this.dataRadioChange(1)
    },
    downFile() {
      const obj = {
        ...this.listQuery,
        syncTmpMerchantId: this.businessInfo.syncTmpMerchantId
      }
      exportMercExpend(removePropertyOfNull(obj)).then(res => {
        console.log('res', res)
        if (res.code === 0) {
          this.$alert(
            '数据导出中，请稍后到数据下载中心进行下载',
            '提示',
            {
              confirmButtonText: '确定',
              center: true,
              type: 'success',
              callback: action => {}
            }
          )
        }
      })
    },
    fetchData() {
      const obj = {
        ...this.listQuery,
        syncTmpMerchantId: this.businessInfo.syncTmpMerchantId
      }
      qryExpendRecord(removePropertyOfNull(obj)).then(res => {
        if (res.code === 0) {
          const { list, total } = res.data.pageList
          console.log('res', res)
          this.list = list
          this.total = total
          this.dataCard = {
            ...res.data,
            pageList: null
          }
        }
      })
    },
    handleSizeChange(val) {
      this.listQuery.pageSize = val
      this.fetchData()
    },
    handleCurrentChange(val) {
      this.listQuery.pageNum = val
      this.fetchData()
    }
  }
}
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
.labelText{
  color: rgb(115, 115, 115);
  font-size: 12px;
}
.common-container{
  // background: #fff;
  border-radius: 5px;
  padding: 20px;
  .dataCard{
    display: flex;
    /deep/.el-card{
      margin: 10px 10px 10px 0;
      min-width: 200px;
      .el-card__body{
        padding: 10px 20px;
        .title{
          border-left: 3px solid #ffb72f;
          padding-left: 5px;
        }
        .money{
          font-size: 20px;
          margin-top: 15px;
        }
        .content{
          margin-top: 15px;
          font-size: 12px;
        }
      }
    }
  }
  .dataList{
    background: #fff;
  }
}
</style>

